<?php

namespace backend\controllers;

use app\models\Engine;
use Yii;
use app\models\Mdsparts;
use app\models\MdspartsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * MdspartsController implements the CRUD actions for Mdsparts model.
 */
class MdspartsController extends Controller
{
    public $layout = "content_parts";
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Mdsparts models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new MdspartsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Mdsparts model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Mdsparts model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Mdsparts();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
//            echo json_encode( $model->getErrors() );
//            die();
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Mdsparts model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {

        $model = $this->findModel($id);
        $engineModel = new Engine();
        $engines = Engine::findALlByPartsId( $id );


        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
                'engineModel' => $engineModel,
                'engines' => $engines,
            ]);
        }
    }

    /**
     * Deletes an existing Mdsparts model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Mdsparts model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Mdsparts the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Mdsparts::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }


    public function actionApi_engine($id)
    {
        $res = Engine::findALlByPartsId( $id );

        header("Content-type:application/json;charset=utf-8");
        echo json_encode( $res );
        die();
    }

    public function actionApi_engine_add(  )
    {
        $post = Yii::$app->request->post();

        if( isset($post['part_id']) && isset($post['engine_id']) ){
            $res = Engine::joinPartsAndEngine( $post['part_id'], $post['engine_id'] );
            header("Content-type:application/json;charset=utf-8");
            echo json_encode( $res );
            die();
        }else{
            die(0);
        }

    }

    public function actionApi_engine_delete( )
    {

        $post = Yii::$app->request->post();

        if( isset($post['part_id']) && isset($post['engine_id']) ){
            $res = Engine::unjoinPartsAndEngine( $post['part_id'], $post['engine_id'] );
            header("Content-type:application/json;charset=utf-8");
            echo json_encode( $res );
            die();
        }else{
            die(0);
        }


    }
}
